package com.yiwei.zhh.util;


import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

/**
 * Created by MasterDong on 16/8/4.
 */
public class JsonUtil {
    public JsonUtil() {
    }

    public static String toJson(Object obj) {
        Gson gson = new Gson();
        return gson.toJson(obj);
    }

    public static <T> T fromJson(String json, Class<?> objType) {
        Gson gson = new Gson();
        return (T) gson.fromJson(json, objType);
    }

    public static String toJsonWidthSpecifiedFields(Object obj) {
        Gson gson = (new GsonBuilder()).excludeFieldsWithoutExposeAnnotation().create();
        return gson.toJson(obj);
    }

    public static <T> T fromJsonWidthSpecifiedFields(String json, Class<?> objType) {
        Gson gson = (new GsonBuilder()).excludeFieldsWithoutExposeAnnotation().create();
        return (T) gson.fromJson(json, objType);
    }
}
